package L619;

import util.ListNode;

/**
 * @program: javase
 * @description:
 * @author: luolidong
 * @create: 2021-06-19 15:49
 * @version: 1.0
 */
public class Demo1 {
    public boolean isPalindrome(ListNode head) {
//        12321
        if(head==null) return false;
//        不能保存原来链表
//        ListNode h=head;
//        先找中点，再反转
        ListNode fast=head,slow=head;
        while (fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        ListNode end=reserve(slow);
        System.out.println(end);
        while(head!=null&&end!=null){
            if(head.val!=end.val){
                return false;
            }
            head=head.next;
            end=end.next;
        }
        return true;
    }
    public ListNode reserve(ListNode root){
        ListNode cur=root,pre=null;
        while(cur!=null){
            ListNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }

    public static void main(String[] args) {
        boolean palindrome = new Demo1().isPalindrome(ListNode.build());
        System.out.println(palindrome);
    }
}
